<?

    define('ROOT_PATH', dirname(__FILE__) . '/');
    define('LIB_PATH', ROOT_PATH . 'lib/');
    require LIB_PATH  . 'contents.php'; 
    
    $config = [
        'userName' => 'szjsnkyy',
        'password' => 'youhua123456',
        'hosType'  => 'jsnk',
    ];
    
    //=============================================登录==========================================
    
        // 登录提交的地址
        $url = 'http://www.d17.cc/d17/pages/user/login.jsp';
        
        // 获取登录表单信息
        $retHtml = Get_Web_Contents($url);
        
        // 校验码
        $regEx = '#name="d17_form_token_" value="(?<val>[^"]+)"#';
        preg_match($regEx, $retHtml['Body'], $formToken);
        
        $post = [
            'd17_form_token_' => $formToken['val'],
            'userName' => $config['userName'],
            'password' => $config['password'],
        ];
        
        // 提交登录数据
        $url = 'http://www.d17.cc/d17/user/login';
        $retHtml = Get_Web_Contents($url, 'POST', http_build_query($post)."&remember=true" ,$retHtml['Cookies']);
        
    //============================================获取img==========================================
    
    echo "type : new\n";
    $src = getimgList(1,$retHtml['Cookies'],'new');
    
    mkdir(ROOT_PATH.'imglist/new/'.$config['hosType'].'/');
    
    file_put_contents(ROOT_PATH.'imglist/new/'.$config['hosType'].'/'.$config['userName'].'.txt',$src);
    
    echo "type : product\n";
    $src = getimgList(1,$retHtml['Cookies'],'product');
    
    mkdir(ROOT_PATH.'imglist/product/'.$config['hosType'].'/');
    
    file_put_contents(ROOT_PATH.'imglist/product/'.$config['hosType'].'/'.$config['userName'].'.txt',$src);
    
    function getimgList($page,$cookie,$type)
    {
        echo "page : $page\n";
        $url = [
            'new' => "http://www.d17.cc/d17/news/newsList/{$page}",
            'product' => "http://www.d17.cc/d17/product/toProductManage/{$page}",
        ];
        
        // 读取页面内容
        $retHtml = Get_Web_Contents($url[$type], 'GET', '', $cookie);

        $retHtml['Body'] = str_ireplace(array("\r\n","\t", "\r","\n"), '', $retHtml['Body']);
        
        preg_match('#条/(.*?)页#',$retHtml['Body'],$count);
        
        // 拼接读取地址 
        $regEx = '#src="http://img[0-9]+.d17.cc/(?<src>[^"]+)"#s';
        
        preg_match_all($regEx, $retHtml['Body'], $src);
        
        $src = implode("\n",$src['src'])."\n";
        
        $src = str_ireplace(['_detail-thumb','_show'],'',$src);
        
        if($page < $count[1]){
            $src .= getimgList(++$page,$cookie,$type);
        }
        
        return $src;
    }
